﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：UserCouponsManage.cs
  //文件功能描述：会员的优惠劵管理；
  //创建人：赵国伟
  //生成时间：2012/03/18 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class UserCouponsManage
    {
        private readonly DALUserCoupons dalUserCoupon = new DALUserCoupons();

        #region "分页返回指定用户的优惠劵列表信息 by 赵国伟 2012-04-24"
        /// <summary>
        /// 分页返回指定用户的优惠劵列表信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<UserCouponInfo> QueryListAsPage(int UserId, AsPage p)
        {
            string cond = "UserID=" + UserId;
            string orderby = "GainDate desc ";
            List<UserCoupons> listOfS = dalUserCoupon.QueryListAsPage(cond, orderby, p.CurPage, p.PageSize, out p.TotalCount);
            List<UserCouponInfo> listOfT = null;
            if (listOfS != null)
            {
                listOfT = new List<UserCouponInfo>();
                CouponsManage bllCoupon = new CouponsManage();
                Dictionary<int, Coupons> dict = bllCoupon.QueryListToHash("CouponID in(Select CouponID From UserCoupons Where " + cond + " )");
                foreach (UserCoupons s in listOfS)
                {
                    UserCouponInfo t = new UserCouponInfo();
                    t.UserCoupon = s;
                    if (dict.ContainsKey(s.CouponID))
                        t.Coupon = dict[s.CouponID];
                    listOfT.Add(t);
                }

            }
            return listOfT;
        }
        #endregion

        


        public UserCouponInfo GetCouponInfoById(int couponid, int userid)
        {
            UserCouponInfo u=null;

            var coupon = dalUserCoupon.QueryList("userid=" + userid + " and CouponId=" + couponid, "");
            if (coupon != null && coupon.Count > 0)
            {
                u = new UserCouponInfo();
                CouponsManage c = new CouponsManage();
                u.Coupon = c.GetCouponInfo(couponid);
                u.UserCoupon = coupon[0];
            }

            return u;
        }


    }
}
